package com.apes.fn.server.workDesk.service;

import afu.org.checkerframework.checker.units.qual.A;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.apes.fn.server.workDesk.model.StockWarring;
import com.apes.fn.server.workDesk.model.UnsalableStock;
import com.apes.fn.server.workDesk.repository.StockWarringRepository;
import com.apes.fn.server.workDesk.repository.UnsalableStockRepository;
import com.apes.fn.server.workDesk.repository.WorkDeskConfigDao;
import com.apes.framework.api.DomainService;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: apes-server
 * @description:
 * @author: lws
 * @create: 2023-02-13 10:28
 **/
@Service("workDeskTask")
public class WorkDeskTask extends DomainService {

    @Autowired
    WorkDeskConfigDao workDeskConfigDao;

    @Autowired
    StockWarringRepository stockWarringRepository;

    @Autowired
    UnsalableStockRepository unsalableStockRepository;

    /**
     * 日结任务-工作台-库存预警-滞销库存
     */
    public void stockRCLTask(SimpleRequest request){
        stockWarring();     // 库存预警
        unsalableStock();   // 滞销库存
    }

    private void stockWarring(){
        JSONArray queryResults = workDeskConfigDao.stockWarring(new JSONObject());
        stockWarringRepository.truncateTable();  // 清空数据
        List<StockWarring> stockWarringList = new ArrayList<>();
        queryResults.stream().forEach(queryResult -> {
            JSONObject jo = (JSONObject) queryResult;
            StockWarring stockWarring = new StockWarring();
            stockWarring.setLocationId(jo.getString("LOCATION_ID"));
            stockWarring.setLocationName(jo.getString("LOCATION_NAME"));
            stockWarring.setDeptId(jo.getString("DEPT_ID"));
            stockWarring.setDeptName(jo.getString("DEPT_NAME"));
            stockWarring.setProductId(jo.getString("PRODUCT_ID"));
            stockWarring.setProductName(jo.getString("PRODUCT_NAME"));
            stockWarring.setUsableQty(jo.getDoubleValue("USABLE_QTY"));
            stockWarring.setSafetyStockQty(jo.getDoubleValue("SAFETY_STOCK_QTY"));
            stockWarring.setSaleSeven(jo.getDoubleValue("SALE_7"));
            stockWarring.setSaleThirty(jo.getDoubleValue("SALE_30"));
            stockWarringList.add(stockWarring);
        });
        stockWarringRepository.saveAll(stockWarringList);
    }

    private void unsalableStock(){
        JSONArray queryResults = workDeskConfigDao.unsalableStock(new JSONObject());
        unsalableStockRepository.truncateTable();   // 清空数据
        List<UnsalableStock> unsalableStockList = new ArrayList<>();
        queryResults.stream().forEach(queryResult -> {
            JSONObject jo = (JSONObject) queryResult;
            UnsalableStock unsalableStock = new UnsalableStock();
            unsalableStock.setDeptId(jo.getString("DEPT_ID"));
            unsalableStock.setDeptName(jo.getString("DEPT_NAME"));
            unsalableStock.setProductId(jo.getString("PRODUCT_ID"));
            unsalableStock.setProductName(jo.getString("PRODUCT_NAME"));
            unsalableStock.setUsableQty(jo.getDoubleValue("USABLE_QTY"));
            unsalableStock.setSafetyStockQty(jo.getDoubleValue("SAFETY_STOCK_QTY"));
            unsalableStock.setSaleSeven(jo.getDoubleValue("SALE_7"));
            unsalableStock.setSaleThirty(jo.getDoubleValue("SALE_30"));
            unsalableStockList.add(unsalableStock);
        });
        unsalableStockRepository.saveAll(unsalableStockList);
    }

}
